using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

using MegaEngine.Source.Tiles;
using MegaEngine.Source.Sprites;
using MegaEngine.Source.GameData;
namespace MegaEngine.Source.Levels
{

    class MetroidLevelEditor : MegaEngineGame
    {
        Sprite[,] bricks;
        long saveTimer = 3000;
        public MetroidLevelEditor()
        {
            tilePaths = new String[2];
            spritePaths = new String[3];
            tileTextures = new Texture2D[2];
            spriteTextures = new Texture2D[3];
            tilePaths[0] = "Content\\Textures\\Tiles\\Black";
            tilePaths[1] = "Content\\Textures\\Tiles\\Black";
            spritePaths[0] = "Content\\Textures\\Sprites\\Metroid\\peach";
            spritePaths[1] = "Content\\Textures\\Sprites\\Metroid\\brick";
            spritePaths[2] = "Content\\Textures\\Sprites\\Metroid\\highlight";
            xBounds = 250; // must be at least 27
            yBounds = 21; // must be at least 21
            bricks = new Sprite[xBounds+1,yBounds+1
                ];
        }

        public override void update(GameTime gameTime)
        {
            saveTimer += gameTime.ElapsedGameTime.Milliseconds;
            playerAvatar.input();
            scrollBackground();
            levelBorderCollisionDetection();
            processInput();
            playerAvatar.bounds.X = (int)(playerAvatar.levelOrigin.X - spriteOrigin.X);
            playerAvatar.bounds.Y = (int)(playerAvatar.levelOrigin.Y - spriteOrigin.Y);
            playerAvatar.update(spriteOrigin.X, spriteOrigin.Y, xBounds * 32, yBounds * 32, gameTime, environment, spriteOrigin);
        }
        private void processInput()
        {
            KeyboardState keyboard = Keyboard.GetState();
            keyboard = Keyboard.GetState();
            int xLoc = (int)((playerAvatar.levelOrigin.X) / 32);
            int yLoc = (int)((playerAvatar.levelOrigin.Y) / 32);
            if(keyboard.IsKeyDown(Keys.Z) && bricks[xLoc, yLoc]==null){
                setEnvironmentPiece(xLoc, yLoc);
            }
            if (keyboard.IsKeyDown(Keys.D) && bricks[xLoc, yLoc] != null)
            {
                removeEnvironmentPiece(xLoc, yLoc);
            }
            if (keyboard.IsKeyDown(Keys.LeftControl) && keyboard.IsKeyDown(Keys.S) && saveTimer >= 3000)
            {
                saveTimer = 0;
                LevelLoader.writeEnvironmentData("Content\\Levels\\Metroid\\level1", environment);
            }
        }
        private void removeEnvironmentPiece(int xLoc, int yLoc)
        {
            Sprite b = bricks[xLoc, yLoc];
            environment.Remove(b);
            bricks[xLoc, yLoc] = null;
        }
        private void setEnvironmentPiece(int xLoc, int yLoc)
        {

            Brick newPiece = new Brick();
            newPiece.levelOrigin.X = playerAvatar.levelOrigin.X;
            newPiece.levelOrigin.Y = playerAvatar.levelOrigin.Y;
            newPiece.texture = spriteTextures[1];
            newPiece.textureID = 4;
            environment.Add(newPiece);
            bricks[xLoc,yLoc] = newPiece;
        }
        private void levelBorderCollisionDetection()
        {
            if (playerAvatar.levelOrigin.X < 0)
            {
                playerAvatar.levelOrigin.X = 0;
            }
            else if (playerAvatar.levelOrigin.X > xBounds * 32)
            {
                playerAvatar.levelOrigin.X = xBounds * 32;
            }
            if (playerAvatar.levelOrigin.Y < 0)
            {
                playerAvatar.levelOrigin.Y = 0;
            }
            else if (playerAvatar.levelOrigin.Y > yBounds * 32)
            {
                playerAvatar.levelOrigin.Y = yBounds * 32;
            }
        }

        public override void init()
        {
            initBackgroundLayer3();
            initViewFrame();
            npcs = new ArrayList();
            environment = new ArrayList();

            playerAvatar = new LevelEditorHighlight();
            playerAvatar.texture = spriteTextures[2];
        }

        private void initBackgroundLayer3()
        {
            backgroundLayer3 = new Texture2D[xBounds, yBounds];
            for (int i = 0; i < xBounds; i++)
            {
                for (int j = 0; j < yBounds; j++)
                {
                    if (i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1)
                    {
                        backgroundLayer3[i, j] = tileTextures[0];
                    }
                    else
                    {
                        backgroundLayer3[i, j] = tileTextures[1];
                    }
                }
            }
        }

        private void initViewFrame()
        {
            viewFrame = new Tile[26, 20];
            for (int i = 0; i < 26; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    viewFrame[i, j] = new Tile();
                }
            }
        }
        public override void renderHud(MegaEngine.Source.Font.XNAFont fontRenderer, bool showFPS, GameTime gameTime)
        {
            if (saveTimer < 3000)
            {
                fontRenderer.OutputText("Level Saved", 480, 0, Color.White);
            }
            base.renderHud(fontRenderer, showFPS, gameTime);
        }
    }

}
